#include <clusnet/net/UdpServer.h>
#include <iostream>

int main(int argc, char* argv[])
{
    InitLogging();
    try
    {
        if (argc < 4)
        {
            LOG(ERROR) << "Usage: UdpServer_test <ip> <port> <thread_num>";
            return 1;
        }
        clusnet::net::UdpServer server(argv[1], std::atoi(argv[2]));
        server.setMessageCallback([](clusnet::net::UdpSessionPtr& session, clusnet::net::UdpUniqDataPacketPtr&& data)
        {
            LOG(INFO) << "recv from " << session->remoteEndpoint().address() << ":" << session->remoteEndpoint().port() << " " << data->dataLength() << " bytes";
            session->safeAsyncWrite(std::move(data));
        });
        server.setWriteCompleteCallback([](clusnet::net::UdpSessionPtr& session, clusnet::net::UdpDataPacketPtr const& data)
        {
            LOG(INFO) << "send to " << session->remoteEndpoint().address() << ":" << session->remoteEndpoint().port() << " " << data->dataLength() << " bytes";
        });
        server.start(std::atoi(argv[3]));

        std::string line;
        while (std::cin >> line)
        {
            if (line == "stop")
            {
                LOG(INFO) << "stop server";
                server.stop();
                break;
            }
        }
    }
    catch (std::exception& e)
    {
        LOG(ERROR) << "Exception: " << e.what();
    }
    return 0;
}
